import { ElMessage } from "element-plus";
import useStore from "@/store/index";
export const getStatusColor = (status) => {
	const statusColors = {
		待下发: "#faad14",
		进行中: "#1890ff",
		工长审核: "#52c41a",
		技管审核: "#722ed1",
		归档: "#8c8c8c",
	};
	return statusColors[status] || "#8c8c8c";
};
export const convertDict = (dictName, type, target) => {
	const store = useStore();
	const targetDict = store.dict.find((item) => item.name === dictName);
	if (!targetDict || !targetDict.children || targetDict.children.length === 0) {
		return null;
	}
	const children = targetDict.children;
	let result = null;
	if (type === "v2l") {
		const matched = children.find((item) => item.value == target);
		result = matched ? matched.label : null;
	} else if (type === "l2v") {
		const matched = children.find((item) => item.label === target);
		result = matched ? matched.value : null;
	}
	return result;
};

export const ElMessageErrorGroup = (message) => {
	return ElMessage({
		message: message,
		grouping: true,
		type: "error",
	});
};

export const errorNotify = (fileName, functionName, error) => {
	const now = new Date();
	const timeStr =
		now.getFullYear() +
		"-" +
		String(now.getMonth() + 1).padStart(2, "0") +
		"-" +
		String(now.getDate()).padStart(2, "0") +
		" " +
		String(now.getHours()).padStart(2, "0") +
		":" +
		String(now.getMinutes()).padStart(2, "0") +
		":" +
		String(now.getSeconds()).padStart(2, "0");
	const errorMessage = error?.message || String(error);
	const backendMsg = error?.response?.data?.msg || "";
	const errorMsg = `[${timeStr}]<br>${fileName}：${functionName}<br>${errorMessage}${backendMsg ? `<br>${backendMsg}` : ""}`;
	console.error(errorMsg, error);
	ElNotification({
		title: "请截图并提报问题工单",
		message: errorMsg,
		type: "error",
		duration: 0,
		dangerouslyUseHTMLString: true,
	});
};

export const clearAllCaches = async () => {
	const store = useStore();
	try {
		store.clearAll();
	} catch (error) {
		errorNotify("Misc", "clearAllCaches", error);
		throw error;
	}
};
